﻿using System;
using System.Collections.Generic;

using NHibernate;

using NHibTest.DBTask.MySQL5.Entity;

namespace NHibTest.DBTask.MySQL5
{
	/// <summary>
	/// 注册会员数据库任务类
	/// </summary>
	public class MemberDBTask
	{
		/// <summary>
		/// 浏览注册会员
		/// </summary>
		private const String HQL_ViewMember = "from MemberEntity entity where entity.ID = :ID";

		#region 类构造器
		/// <summary>
		/// 类默认构造器
		/// </summary>
		public MemberDBTask()
		{
		}
		#endregion

		/// <summary>
		/// 浏览注册会员
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public MemberEntity ViewMember(int ID)
		{
			using (ISession session = NHibernateHelper.CreateSession())
			{
				MemberEntity entity = session.CreateQuery(MemberDBTask.HQL_ViewMember)
					.SetParameter("ID", ID)
					.UniqueResult<MemberEntity>();

				return entity;
			}
		}

		/// <summary>
		/// 更新注册会员
		/// </summary>
		/// <param name="newEntity"></param>
		public void UpdateMember(MemberEntity newEntity)
		{
			if (newEntity == null)
			{
				return;
			}

			using (ISession session = NHibernateHelper.CreateSession())
			using (ITransaction tranx = session.BeginTransaction())
			{
				// 开始事务
				tranx.Begin();

				// 更新
				session.Update(newEntity);

				// 提交事务
				tranx.Commit();
			}
		}
	}
}
